#include "window.h"

static inline GtkWidget *menubar_add_item(GtkMenuShell *menu, const gchar *label);
static inline GtkWidget *menubar_add_menu(GtkMenuShell *menubar, const gchar *label);

static void menubar_add_fileMenu(MenuBar *bar);
static void menubar_add_editMenu(MenuBar *bar);
static void menubar_add_helpMenu(MenuBar *bar);

static void show_about();

/*槽*/
void on_aboutMenuItem_activate(GtkWidget *widget, gpointer window);

MenuBar *menubar_new(gpointer w)
{
    MenuBar *menubar;

    menubar = (MenuBar *)g_malloc(sizeof(MenuBar));

    menubar->menubar = gtk_menu_bar_new();

    menubar_add_fileMenu(menubar);
    menubar_add_helpMenu(menubar);

    return menubar;
}

void menubar_add_fileMenu(MenuBar *bar)
{
    /* add menu */
    bar->fileMenu = menubar_add_menu(GTK_MENU_SHELL(bar->menubar), "File");

    /* exit item */
    bar->exitMenuItem = menubar_add_item(GTK_MENU_SHELL(bar->fileMenu), "Exit");
    g_signal_connect_swapped(G_OBJECT(bar->exitMenuItem), "activate", G_CALLBACK(gtk_main_quit), NULL);
}

void menubar_add_helpMenu(MenuBar *bar)
{
    /* add menu */
    bar->helpMenu = menubar_add_menu(GTK_MENU_SHELL(bar->menubar), "Help");

    /*about item*/
    bar->aboutMenuItem = menubar_add_item(GTK_MENU_SHELL(bar->helpMenu), "About");
    g_signal_connect(G_OBJECT(bar->aboutMenuItem), "activate", G_CALLBACK(on_aboutMenuItem_activate), NULL);

//    gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
//    g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(on_window_press_event), (gpointer)window);
}

GtkWidget *menubar_add_menu(GtkMenuShell *menubar, const gchar *label)
{
    GtkWidget *menu;
    GtkWidget *menuItem;

    menu = gtk_menu_new();
    menuItem = gtk_menu_item_new_with_label(label);

    gtk_menu_shell_append(menubar, menuItem);
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuItem), menu);

    return menu;
}

GtkWidget *menubar_add_item(GtkMenuShell *menu, const gchar *label)
{
    GtkWidget *menuItem = gtk_menu_item_new_with_label(label);
    gtk_menu_shell_append(menu, menuItem);
    return menuItem;
}

void on_aboutMenuItem_activate(GtkWidget *widget, gpointer window)
{
    show_about();
}

void show_about()
{
    GdkPixbuf *batteryPixbuf;
    GtkWidget *aboutDialog;

    batteryPixbuf = gdk_pixbuf_new_from_file("battery.png", NULL);

    aboutDialog = gtk_about_dialog_new();
    gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(aboutDialog), "Battery");
    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(aboutDialog), "1.1");
    gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(aboutDialog), "(c) JakeYang");
    gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(aboutDialog), "Battery is a simple tool for battery checking.");
    gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(aboutDialog), "http://add358.cublog.cn");
    gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(aboutDialog), batteryPixbuf);

    g_object_unref(batteryPixbuf), batteryPixbuf = NULL;

    gtk_dialog_run(GTK_DIALOG(aboutDialog));
    gtk_widget_destroy(aboutDialog);
}
